package com.mitake.utils;

import java.util.Calendar;

public class DateUtility
{
	public static String getBeforeDate(int day, String separate)
	{
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, day);
				  
		StringBuffer now = new StringBuffer();
		now.append(getYear(cal)).append(separate);
		now.append(getMonth(cal)).append(separate);
		now.append(getDay(cal));
		return now.toString();
	}
	
	public static String getNowDate(String separate)
	{
		Calendar cal = Calendar.getInstance();

		StringBuffer now = new StringBuffer();
		now.append(getYear(cal)).append(separate);
		now.append(getMonth(cal)).append(separate);
		now.append(getDay(cal));
		return now.toString();
	}

	public static String getNowDate()
	{
		Calendar cal = Calendar.getInstance();

		StringBuffer now = new StringBuffer();
		now.append(getYear(cal));
		now.append(getMonth(cal));
		now.append(getDay(cal));
		return now.toString();
	}
	
	public static String getTimestamp()
	{
		Calendar cal = Calendar.getInstance();

		StringBuffer now = new StringBuffer();
		now.append(getYear(cal));
		now.append(getMonth(cal));
		now.append(getDay(cal));
		now.append(getHour(cal));
		now.append(getMinute(cal));
		now.append(getSecond(cal));
		now.append(getMilliSecond(cal));
		return now.toString();
	}

	public static String getDBDateTime()
	{
		Calendar cal = Calendar.getInstance();

		StringBuffer now = new StringBuffer();
		now.append(getYear(cal)).append("/");
		now.append(getMonth(cal)).append("/");
		now.append(getDay(cal));
		now.append(" ");
		now.append(getHour(cal));
		now.append(":");
		now.append(getMinute(cal));
		now.append(":");
		now.append(getSecond(cal));
		return now.toString();
	}

	private static String getYear(final Calendar cal)
	{
		return String.valueOf(cal.get(Calendar.YEAR));
	}

	private static String getMonth(final Calendar cal)
	{
		return check(String.valueOf(cal.get(Calendar.MONTH) + 1));
	}

	private static String getDay(final Calendar cal)
	{
		return check(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)));
	}

	private static String getHour(final Calendar cal)
	{
		return check(String.valueOf(cal.get(Calendar.HOUR_OF_DAY)));
	}

	private static String getMinute(final Calendar cal)
	{
		return check(String.valueOf(cal.get(Calendar.MINUTE)));
	}

	private static String getSecond(final Calendar cal)
	{
		return check(String.valueOf(cal.get(Calendar.SECOND)));
	}

	private static String getMilliSecond(final Calendar cal)
	{
		return String.valueOf(cal.get(Calendar.MILLISECOND));
	}
	
	private static String check(final String value)
	{
		StringBuffer lastValue = new StringBuffer();
		
		if(value.length() == 1)
		{
			lastValue.append("0").append(value);
		}
		else if(value.length() == 0)
		{
			lastValue.append("00").append(value);
		}
		else
		{
			lastValue.append(value);
		}

		return lastValue.toString();
	}
}
